<template>
  <CommonModule
    v-bind="$attrs"
    :columns="columns"
    :fetchList="fetchList"
  />
</template>
<script>
import CommonModule from './CommonModule';
import { ACCEPTANCE_STANDARD_COLUMNS } from './constants';
import { getDetailByRecordNoService } from '@/api/commodity/commodityAccessManage/commodityAdd';
export default {
  components: {
    CommonModule
  },
  data () {
    return {
      columns: ACCEPTANCE_STANDARD_COLUMNS,
    }
  },
  methods: {
    // 获取表格数据
    async fetchList (data, loadingFn) {
      const res = await getDetailByRecordNoService(data, loadingFn)
      const results = this.getTableListByAcceptStandardTab(res?.retData?.results ?? [])
      return {
        results,
        totalNum: res?.retData?.totalNum
      }
    },
    // 获取验收事项表格数据
    getTableListByAcceptStandardTab (results) {
      const updateList = _.cloneDeep(results).reduce((value, item, index) => {
        // 新建表头。并准备一行数据的动态列
        const curRow = _.pick(item, [
          'productName', 'brandName', 'categoryName',
          'spuNo', 'skuNo', 'checkTime', 'specDetailStr',
          'checkFrequency', 'itemList'
        ])
        // 将行，添加到展示列表
        value.push(this.getCurRowByAcceptStandard(curRow, item.itemList, index));
        return value;
      }, [])
      return updateList
    },
    // 获取验收事项表格数据
    getTableListByAcceptStandardTab (retData) {
      return _.cloneDeep(retData).reduce((value, item, index) => {
        const curRow = {
          productName: item.productName,
          brandName: item.brandName,
          categoryName: item.categoryName,
          spuNo: item.spuNo,
          skuNo: item.skuNo,
          checkTime: item.checkTime,
          specDetailStr: item.specDetailStr,
          checkFrequency: item.checkFrequency,
          itemList: item.itemList
        };
        // 新建表头。并准备一行数据的动态列
        // 将行，添加到展示列表
        value.push(this.getCurRowByAcceptStandard(curRow, item.itemList, index))
        return value
      }, [])
    },
    // 获取验收标准当前row
    getCurRowByAcceptStandard (curRow, itemList, oneIndex) {
      if (!itemList) { return [] }
      const columns = _.cloneDeep(ACCEPTANCE_STANDARD_COLUMNS)
      // 获取行内动态列的信息
      const curRows = itemList.reduce((value, item, index) => {
        // 表头，遍历第1行的动态列时，才追加到表头
        if (oneIndex == 0) {
          this.updateTableColumnsByAcceptStandard(columns, item, index)
        }
        // 将行内动态列的信息，平铺到行
        value[`checkCount${index}`] = item.checkCount;
        value[`standardValue${index}`] = item.standardValue;
        value[`upDiffValue${index}`] = item.upDiffValue;
        value[`downDiffValue${index}`] = item.downDiffValue;
        value[`checkMethod${index}`] = item.checkMethod;
        value[`checkItemType${index}`] = item.checkItemType;
        return value
      }, curRow)
      if (oneIndex == 0) {
        this.columns = [...columns]
      }
      return curRows
    },
    // 更新验收标准页签表列
    updateTableColumnsByAcceptStandard (columns, item, index) {
      const hasDiffValueList = [
        { key: 'checkCount', title: '验收次数' },
        { key: 'standardValue', title: '标准值' },
        { key: 'upDiffValue', title: '上差值' },
        { key: 'downDiffValue', title: '下差值' },
        { key: 'checkMethod', title: '验收方法' },
      ];
      const commonList = [
        { key: 'checkCount', title: '验收次数' },
        { key: 'standardValue', title: '标准值' },
        { key: 'checkMethod', title: '验收方法' },
      ]
      if (item.checkItemType !== '2') {
        columns.push(
          this.getExtColumnByAcceptStandard(item, hasDiffValueList, index)
        )
      } else {
        columns.push(
          this.getExtColumnByAcceptStandard(item, commonList, index)
        )
      }
    },
    // 获取验收事项额外扩展表列
    getExtColumnByAcceptStandard (innerEle, list, index) {
      return {
        title: innerEle.checkItemName,
        required: innerEle.isNeed === '1',
        key: innerEle.checkItemUuid,
        width: 150,
        align: 'center',
        children: list.map(item => ({
          title: item.title,
          align: 'center',
          key: `${item.key}${index}`
        })).filter(item => item)
      }
    },
  }
}
</script>
